﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OrganizeYourSearch
{
    /// <summary>
    /// ========================================
    /// .NET Framework 3.0 Custom Control
    /// ========================================
    ///
    /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
    ///
    /// Step 1a) Using this custom control in a XAML file that exists in the current project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:CloseableTabItemDemo"
    ///
    ///
    /// Step 1b) Using this custom control in a XAML file that exists in a different project.
    /// Add this XmlNamespace attribute to the root element of the markup file where it is 
    /// to be used:
    ///
    ///     xmlns:MyNamespace="clr-namespace:CloseableTabItemDemo;assembly=CloseableTabItemDemo"
    ///
    /// You will also need to add a project reference from the project where the XAML file lives
    /// to this project and Rebuild to avoid compilation errors:
    ///
    ///     Right click on the target project in the Solution Explorer and
    ///     "Add Reference"->"Projects"->[Browse to and select this project]
    ///
    ///
    /// Step 2)
    /// Go ahead and use your control in the XAML file. Note that Intellisense in the
    /// XML editor does not currently work on custom controls and its child elements.
    ///
    ///     <MyNamespace:CloseableTabItem/>
    ///
    /// </summary>
    
     
    public class CloseableTabItem : TabItem
    {
        
        //public TabTypes tabType;

        private TextBox _tb;
        
        static CloseableTabItem()
        {            //This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
            //This style is defined in themes\generic.xaml
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CloseableTabItem),
                new FrameworkPropertyMetadata(typeof(CloseableTabItem)));
            
        }

       
        #region CLOSE TAB ITEM EVENT DESCRIPTION
        public static readonly RoutedEvent CloseTabEvent =
            EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
                typeof(RoutedEventHandler), typeof(CloseableTabItem));

        public event RoutedEventHandler CloseTab
        {
            add { AddHandler(CloseTabEvent, value); }
            remove { RemoveHandler(CloseTabEvent, value); }
        }

        void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
        }
        #endregion


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _tb = new TextBox();
            Button closeButton = base.GetTemplateChild("PART_Close") as Button;
            if (closeButton != null)
                closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);

            TextBox tbContent = base.GetTemplateChild("Content_TextBox") as TextBox;
            if (tbContent != null)
            {
                _tb = tbContent;
                tbContent.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(tbContent_PreviewMouseLeftButtonDown);
                tbContent.PreviewMouseDoubleClick += new MouseButtonEventHandler(tbContent_PreviewMouseDoubleClick);
                tbContent.LostFocus += new RoutedEventHandler(tbContent_LostFocus);
                tbContent.KeyDown += new KeyEventHandler(tbContent_KeyDown);
                tbContent.Cursor = Cursors.Arrow;
                this.Focusable = true;
                this.PreviewMouseDown += new MouseButtonEventHandler(CloseableTabItem_PreviewMouseDown);
             
            }

            this.DragOver += new DragEventHandler(CloseableTabItem_DragOver);
            
            
        }

        void CloseableTabItem_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
           
            _tb.Focusable = false;
            _tb.Cursor = Cursors.Arrow;
            _tb.SelectionLength = 0;
            
        }



        void CloseableTabItem_DragOver(object sender, DragEventArgs e)
        {
            
        }

        #region TAB ITEM'S TITLE TEXTBOX EVENTS REGION
        //If enter is pressed, get the keyboard focus out of the textbox
        void tbContent_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.Enter))
            {
                TextBox tbContent=e.Source as TextBox;
                if (tbContent != null)
                {
                    this.Focus();
                    tbContent.Focusable = false;
                }
            }
        }

        // if focus is out of the textbox, change cursor style for textbox to arrow, and set previously mouse was not clicked
        void tbContent_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tbContent = e.Source as TextBox;
            if (tbContent != null)
            {
                tbContent.Cursor = Cursors.Arrow;
            }
            
        }

        // Mouse click events
        void tbContent_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
            TextBox tbContent = e.Source as TextBox;
            if (tbContent != null)
            {
                    
                    tbContent.Focusable = false;
                    tbContent.Cursor = Cursors.Arrow;
            }
            
        }

        void tbContent_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            TextBox tbContent = e.Source as TextBox;
            if (tbContent != null)
            { // If this is not the first time you clicked the textbox, the focus is set to the textbox and its
                //cursor is changed to textbox style IBEAM.
                    tbContent.Focusable = true;
                    tbContent.Focus();
                    tbContent.SelectAll();
                    tbContent.Cursor = Cursors.IBeam;
                    e.Handled = true;     
            }
        }





        #endregion



    }
}
